package com.smartmedical.doctor.medic.service.ipml;

import com.smartmedical.common.core.domain.HealthR;
import com.smartmedical.common.core.utils.OssUtil;
import com.smartmedical.common.security.service.TokenService;
import com.smartmedical.common.security.utils.SecurityUtils;
import com.smartmedical.doctor.medic.domain.dto.param.DoctorIdCardParam;
import com.smartmedical.doctor.medic.domain.vo.*;
import com.smartmedical.doctor.medic.mapper.MedicVerifyMapper;
import com.smartmedical.doctor.medic.service.MedicVerifyService;
import com.smartmedical.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class MedicVerifyServicempl implements MedicVerifyService {
    @Autowired
    HttpServletRequest request;
    @Autowired
    MedicVerifyMapper medicVerifyMapper;

    @Override
    public HealthR<DoctorInfoVo> findDoctorById() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();
        DoctorInfoVo doctorInfoVo = medicVerifyMapper.findDoctorById(userId);
        if (doctorInfoVo != null) {
            return HealthR.ok(doctorInfoVo, "查询成功");
        }
        return HealthR.fail("查询失败");
    }

    @Override
    public HealthR<String> uploadImagePic(MultipartFile imagePic) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();

        try {
            // 上传文件至OSS
            String path = OssUtil.uploadMultipartFile(imagePic);

            // 修改医生信息的图片
            Integer result = medicVerifyMapper.uploadImagePic(path, userId);

            if (result > 0) {
                // 返回更多信息，如访问URL等
                return HealthR.ok(path, "上传成功");
            } else {
                return HealthR.fail("上传失败");
            }
        } catch (Exception e) {
            // 处理文件上传或数据库操作异常
            return HealthR.fail("上传失败：" + e.getMessage());
        }
    }

    @Override
    public HealthR chooseImagePic(String imagePic) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();

        // 修改医生信息的图片
        Integer result = medicVerifyMapper.uploadImagePic(imagePic, userId);
        if (result > 0) {
            return HealthR.ok("上传成功");
        }
        return HealthR.fail("上传失败");
    }

    @Override
    public HealthR bindDoctorBankCard(String bankCardNumber, String bankName, Integer bankCardType) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();
        if (!bankCardNumberisTrue(bankCardNumber)) {
            return HealthR.fail("银行卡号格式不正确");
        }
        //查重银行卡号
        if (medicVerifyMapper.findDoctorIdCardByIdNumber(bankCardNumber)) {
            return HealthR.fail("银行卡号重复");
        }
        Integer add = medicVerifyMapper.bindDoctorBankCard(userId, bankCardNumber, bankName, bankCardType);
        if (add > 0) {
            return HealthR.ok("绑定成功");
        } else {
            return HealthR.fail("绑定失败");
        }
    }

    @Override
    public HealthR<List<DoctorBankCardVo>> findDoctorBankCardById() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();
        List<DoctorBankCardVo> list = medicVerifyMapper.findDoctorBankCardById(userId);

        if (list != null) {
            return HealthR.ok(list, "查询成功");
        }
        return HealthR.fail("查询失败");
    }

    @Override
    public HealthR bindDoctorIdCard(DoctorIdCardParam doctorIdCardParam) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();
        doctorIdCardParam.setDoctorId(userId);
        if (!idCardisTrue(doctorIdCardParam.getIdNumber())) {
            return HealthR.fail("身份证号格式不正确");
        }
        //查重身份证号
        if (medicVerifyMapper.findDoctorIdCardByIdCard(doctorIdCardParam.getIdNumber())) {
            return HealthR.fail("该身份证号已存在");
        }
        Integer add = medicVerifyMapper.bindDoctorIdCard(doctorIdCardParam);
        if (add > 0) {
            return HealthR.ok("绑定成功");
        }
        return HealthR.fail("绑定失败");
    }

    @Override
    public HealthR<DoctorIdCardVo> findDoctorIdCardInfo() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();
        DoctorIdCardVo doctorIdCard = medicVerifyMapper.findDoctorIdCardInfo(userId);
        if (doctorIdCard != null) {
            return HealthR.ok(doctorIdCard, "查询成功");
        }

        return HealthR.fail("查询失败");
    }

    @Override
    public HealthR<List<DoctorDrawRecordVo>> findDoctorDrawRecordList() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();
        List<DoctorDrawRecordVo> list = medicVerifyMapper.findDoctorDrawRecordList(userId);
        if (list != null) {
            return HealthR.ok(list, "查询成功");
        }
        return HealthR.fail("查询失败");
    }

    @Override
    public HealthR<List<DoctorIncomeRecordVo>> findDoctorIncomeRecordList() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();
        List<DoctorIncomeRecordVo> list = medicVerifyMapper.findDoctorIncomeRecordList(userId);
        if (list != null) {
            return HealthR.ok(list, "查询成功");
        }
        return HealthR.fail("查询失败");
    }

    @Override
    public HealthR<DoctorWalletVo> findDoctorWallet() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();
        DoctorWalletVo doctorWallet = medicVerifyMapper.findDoctorWallet(userId);
        //绑定银行卡
        List<DoctorBankCardVo> doctorBankCardById = medicVerifyMapper.findDoctorBankCardById(userId);
        if (doctorBankCardById.size() > 0) {
            doctorWallet.setWhetherBindBankCard(1);
        }
        //绑定身份证
        DoctorIdCardVo doctorIdCardInfo = medicVerifyMapper.findDoctorIdCardInfo(userId);
        if (doctorIdCardInfo != null) {
            doctorWallet.setWhetherBindIdCard(1);
        }
        if (doctorWallet != null) {
            return HealthR.ok(doctorWallet, "查询成功");
        }
        return HealthR.fail("查询失败");
    }

    @Override
    public HealthR drawCash(Integer money) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();
        //绑定银行卡
        List<DoctorBankCardVo> doctorBankCardById = medicVerifyMapper.findDoctorBankCardById(userId);
        if (doctorBankCardById.size() <= 0) {
            return HealthR.fail("请绑定银行卡");
        }
        //绑定身份证
        DoctorIdCardVo doctorIdCardInfo = medicVerifyMapper.findDoctorIdCardInfo(userId);
        if (doctorIdCardInfo == null) {
            return HealthR.fail("请绑定身份证");
        }
        DoctorWalletVo doctorWallet = medicVerifyMapper.findDoctorWallet(userId);
        if (money > doctorWallet.getBalance()) {
            return HealthR.fail("余额不足");
        }
        Integer result = medicVerifyMapper.drawCash(money, userId);
        if (result > 0) {
            //提现成功添加记录（记录进度）
            return HealthR.ok("提现成功");
        }
        return HealthR.fail("提现失败");
    }

    @Override
    public HealthR addDoctorPushToken(String token) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();
        if (token == null) {
            return HealthR.fail("token不能为空");
        }
        Integer add = medicVerifyMapper.addDoctorPushToken(token, userId);
        if (add > 0) {
            return HealthR.ok("添加成功");
        }
        return HealthR.fail("添加失败");
    }

    @Override
    public HealthR<List<DoctorEvaluateVo>> findDoctorGiftList() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();
        List<DoctorEvaluateVo> list = medicVerifyMapper.findDoctorGiftList(userId);
        if (list != null) {
            return HealthR.ok(list, "查询成功");
        }
        return HealthR.fail("查询失败");
    }

    @Override
    public HealthR<List<DoctorSystemNoticeVo>> findDoctorSystemNoticeList() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();
        List<DoctorSystemNoticeVo> list = medicVerifyMapper.findDoctorSystemNoticeList(userId);
        if (list != null) {
            return HealthR.ok(list, "查询系统消息成功");
        }
        return HealthR.fail("查询系统消息失败");
    }

    @Override
    public HealthR<List<DoctorSystemNoticeVo>> findDoctorInquiryNoticeList() {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();
        List<DoctorSystemNoticeVo> list = medicVerifyMapper.findDoctorInquiryNoticeList(userId);
        if (list != null) {
            return HealthR.ok(list, "查询问诊消息成功");
        }
        return HealthR.fail("查询问诊消息失败");
    }

    @Override
    public HealthR<List<DoctorSystemNoticeVo>> findDoctorHealthyCurrencyNoticeList() {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();
        List<DoctorSystemNoticeVo> list = medicVerifyMapper.findDoctorHealthyCurrencyNoticeList(userId);
        if (list != null) {
            return HealthR.ok(list, "查询H币消息成功");
        }
        return HealthR.fail("查询H币消息失败");
    }

    @Override
    public HealthR<List<DoctorNoticeReadNumVo>> findDoctorNoticeReadNum() {
        DoctorNoticeReadNumVo doctorNoticeReadNumVo1 = new DoctorNoticeReadNumVo();
        List<DoctorNoticeReadNumVo> list = new ArrayList<>();
        //获取用户id
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();
        //查询系统消息未读数
        Integer systemNotice = medicVerifyMapper.findDoctorSystemNoticeReadNum(userId);
        doctorNoticeReadNumVo1.setNoticeType(1);
        doctorNoticeReadNumVo1.setNotReadNum(systemNotice);
        list.add(doctorNoticeReadNumVo1);
        //查询问诊消息未读数
        DoctorNoticeReadNumVo doctorNoticeReadNumVo2 = new DoctorNoticeReadNumVo();
        Integer inquiryNotice = medicVerifyMapper.findDoctorInquiryNoticeReadNum(userId);
        doctorNoticeReadNumVo2.setNoticeType(2);
        doctorNoticeReadNumVo2.setNotReadNum(inquiryNotice);
        list.add(doctorNoticeReadNumVo2);
        //查询H币消息未读数
        DoctorNoticeReadNumVo doctorNoticeReadNumVo3 = new DoctorNoticeReadNumVo();
        Integer healthyCurrencyNotice = medicVerifyMapper.findDoctorHealthyCurrencyNoticeReadNum(userId);
        doctorNoticeReadNumVo3.setNoticeType(3);
        doctorNoticeReadNumVo3.setNotReadNum(healthyCurrencyNotice);
        list.add(doctorNoticeReadNumVo3);
        return HealthR.ok(list, "查询成功");
    }


    @Override
    public HealthR modifyAllStatus() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();

        //修改消息状态为全部已读

        Integer systemNotice = medicVerifyMapper.modifyAllStatus(userId);


        if (systemNotice > 0) {
            return HealthR.ok("修改成功");
        }
        return HealthR.fail("修改失败");

    }

    @Override
    public HealthR<List<UserAdoptCommentVo>> findMyAdoptedCommentList() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();
        List<UserAdoptCommentVo> list = medicVerifyMapper.findMyAdoptedCommentList(userId);
        if (list != null) {
            return HealthR.ok(list, "查询成功");
        }
        return HealthR.fail("查询失败");
    }

    private boolean idCardisTrue(String idNumber) {
        Pattern compile = Pattern.compile("^[1-9]\\d{5}(?:18|19|20)\\d{2}(?:0\\d|10|11|12)(?:0[1-9]|[1-2]\\d|30|31)\\d{3}[\\dXx]$");
        return compile.matcher(idNumber).matches();

    }

    private boolean bankCardNumberisTrue(String bankCardNumber) {
        Pattern compile = Pattern.compile("^[1-9]\\d{9,29}$");
        return compile.matcher(bankCardNumber).matches();
    }


}
